package main

import (
	"bufio"
	"encoding/json"
	"fmt"
	"github.com/hoisie/redis"
	"io"
	"os"
	"strconv"
	"time"
	//"io/ioutil"
	//"errors"
	. "message"
	"reflect"
)

func verifyTableAdzone(messageStr string) {
	var adzoneResult AdzoneContentStruct
	var adzoneMessageStruct AdzoneMessageStruct
	//var val []byte
	var err error

	json.Unmarshal([]byte(messageStr), &adzoneMessageStruct)

	//lpush message_queue "{\"table_name\":\"ad\",\"status\":\"update\",\"content\":{\"id\":\"1\",\"ad_template_id\":\"2\"}}"
	err = clientPush.Lpush("message_queue", []byte(messageStr))

	if err != nil {
		fmt.Println("Lpush failed%s", err.Error())
	}

	//wait dispatcher process the message_queue
	time.Sleep(5 * time.Second)

	key := adzoneMessageStruct.TableName + "." + strconv.Itoa(adzoneMessageStruct.Id)

	if ok, _ := clientGet.Exists(key); !ok {
		fmt.Println("the key " + key + " is not exists ")
	} else {
		fmt.Println("the key " + key + " is exists. PASSED")
	}

	if typ, err := clientGet.Type(key); err != nil || typ != "string" {
		fmt.Println("the key "+key+" type failed%s", typ)
	}

	if values, err := clientGet.Get(key); err != nil {
		fmt.Println("get the key " + key + " value failed")
	} else {
		fmt.Fprintf(os.Stdout, "%s", values)
		json.Unmarshal(values, &adzoneResult)
	}

	k := reflect.TypeOf(adzoneResult)
	//v := reflect.ValueOf(adzoneResult)

	// type T struct {
	//     A int
	//     B string
	// }
	// t := T{203, "mh203"}
	// s := reflect.ValueOf(&t).Elem()
	// typeOf T := s.Type()
	// for i := 0; i < s.NumField(); i++ {
	//     f := s.Field(i)
	//     fmt.Printf("%d: %s %s = %v\n", i,
	//     typeOf T.Field(i).Name, f.Type(), f.Interface())
	// }

	for i := 0; i < k.NumField(); i++ {
		fmt.Printf("%s \n", k.Field(i).Name)

		switch k.Field(i).Name {
		case "MediaId":
			if adzoneResult.MediaId == adzoneMessageStruct.Content.MediaId {
				fmt.Println("the key  MediaId= " + strconv.Itoa(adzoneResult.MediaId) + " is PASSED")
			}
		case "PublisherId":
			if adzoneResult.PublisherId == adzoneMessageStruct.Content.PublisherId {
				fmt.Println("the key PublisherId= " + strconv.Itoa(adzoneResult.PublisherId) + " is PASSED")
			}
		case "DefaultMaterialId":
			if adzoneResult.DefaultMaterialId == adzoneMessageStruct.Content.DefaultMaterialId {
				fmt.Println("the key DefaultMaterialId " + strconv.Itoa(adzoneResult.DefaultMaterialId) + " is PASSED")
			}
		case "Bidfloor":
			if adzoneResult.Bidfloor == adzoneMessageStruct.Content.Bidfloor {
				fmt.Println("the key Bidfloor " + strconv.Itoa(adzoneResult.Bidfloor) + " is PASSED")
			}
		case "RollType":
			if adzoneResult.RollType == adzoneMessageStruct.Content.RollType {
				fmt.Println("the key RollType " + strconv.Itoa(adzoneResult.RollType) + " is PASSED")
			}
		case "AdModelId":
			if len(adzoneResult.AdModelId) != len(adzoneMessageStruct.Content.AdModelId) {
				fmt.Println("the key AdModelId is FAILED")
				continue
			}

			for i := 0; i < len(adzoneResult.AdModelId); i++ {
				if adzoneResult.AdModelId[i] == adzoneMessageStruct.Content.AdModelId[i] {
					fmt.Println("the key AdModelId " + strconv.Itoa(i) + " " + strconv.Itoa(adzoneResult.AdModelId[i]) + " is PASSED")
				}
			}

		case "OnlineStatus":
			if adzoneResult.OnlineStatus == adzoneMessageStruct.Content.OnlineStatus {
				fmt.Println("the key OnlineStatus " + strconv.Itoa(adzoneResult.OnlineStatus) + " is PASSED")
			}
		case "Width":
			if adzoneResult.Width == adzoneMessageStruct.Content.Width {
				fmt.Println("the key Width " + strconv.Itoa(adzoneResult.Width) + " is PASSED")
			}
		case "Height":
			if adzoneResult.Height == adzoneMessageStruct.Content.Height {
				fmt.Println("the key Height " + strconv.Itoa(adzoneResult.Height) + " is PASSED")
			}
		case "MaxDuration":
			if adzoneResult.MaxDuration == adzoneMessageStruct.Content.MaxDuration {
				fmt.Println("the key MaxDuration " + strconv.Itoa(adzoneResult.MaxDuration) + " is PASSED")
			}
		case "MinDuration":
			if adzoneResult.MinDuration == adzoneMessageStruct.Content.MinDuration {
				fmt.Println("the key MinDuration " + strconv.Itoa(adzoneResult.MinDuration) + " is PASSED")
			}
		case "Exposure":
			if len(adzoneResult.Exposure) != len(adzoneMessageStruct.Content.Exposure) {
				fmt.Println("the key Exposure is FAILED")
				continue
			}

			for i := 0; i < len(adzoneResult.Exposure); i++ {
				if adzoneResult.Exposure[i] == adzoneMessageStruct.Content.Exposure[i] {
					fmt.Println("the key Exposure " + strconv.Itoa(i) + "  " + adzoneResult.Exposure[i] + " is PASSED")
				}
			}
		case "Click":
			if len(adzoneResult.Click) != len(adzoneMessageStruct.Content.Click) {
				fmt.Println("the key Click is FAILED")
				continue
			}

			for i := 0; i < len(adzoneResult.Click); i++ {
				if adzoneResult.Click[i] == adzoneMessageStruct.Content.Click[i] {
					fmt.Println("the key Click " + strconv.Itoa(i) + " " + adzoneResult.Click[i] + " is PASSED")
				}
			}

		default:
			fmt.Println("the key " + k.Field(i).Name + " not supported now")
		}
	}

	// keys, err := client.Keys("*")

	// if err != nil {
	//     println("Redis-dump failed%s", err)
	//     return
	// }

	// for _, key := range keys {
	//     typ, _ := client.Type(key)

	//     if typ == "string" {
	//         data, _ := client.Get(key)
	//         fmt.Fprintf(output, "SET %s %d\r\n%s\r\n", key, len(data), data)
	//     } else if typ == "list" {
	//         llen, _ := client.Llen(key)
	//         for i := 0; i < llen; i++ {
	//             data, _ := client.Lindex(key, i)
	//             fmt.Fprintf(output, "RPUSH %s %d\r\n%s\r\n", key, len(data), data)
	//         }
	//     } else if typ == "set" {
	//         members, _ := client.Smembers(key)
	//         for _, data := range members {
	//             fmt.Fprintf(output, "SADD %s %d\r\n%s\r\n", key, len(data), data)
	//         }
	//     }
	// }

}

func verifyTableAdzoneBlacklist(messageStr string) {
	var adzoneBlacklist AdzoneBlacklistMessageStruct

	json.Unmarshal([]byte(messageStr), &adzoneBlacklist)

	err := clientPush.Lpush("message_queue", []byte(messageStr))

	if err != nil {
		fmt.Println("Lpush failed%s", err.Error())
	}

	//wait dispatcher process the message_queue
	time.Sleep(5 * time.Second)

	key := adzoneBlacklist.TableName + "." + strconv.Itoa(adzoneBlacklist.Id)

	if ok, _ := clientGet.Exists(key); !ok {
		fmt.Println("the key " + key + " is not exists ")
	} else {
		fmt.Println("the key " + key + " is exists. PASSED")
	}

	if typ, err := clientGet.Type(key); err != nil || typ != "set" {
		fmt.Println("the key "+key+" type failed%s", typ)
	}

	if members, _ := clientGet.Smembers(key); len(members) != len(adzoneBlacklist.Content.Array) {
		fmt.Println("the key " + key + " length failed")
	}

	for i := 0; i < len(adzoneBlacklist.Content.Array); i++ {
		if b, _ := clientGet.Sismember(key, []byte(strconv.Itoa(adzoneBlacklist.Content.Array[i]))); b {
			fmt.Println("the key " + key + " has this value " + strconv.Itoa(adzoneBlacklist.Content.Array[i]) + " PASSED")
		}
	}
}

func verifyTableAdzoneMaterialType(messageStr string) {
	var AdzoneMaterialType AdzoneMaterialTypeMessageStruct

	json.Unmarshal([]byte(messageStr), &AdzoneMaterialType)

	err := clientPush.Lpush("message_queue", []byte(messageStr))

	if err != nil {
		fmt.Println("Lpush failed%s", err.Error())
	}

	//wait dispatcher process the message_queue
	time.Sleep(5 * time.Second)

	key := AdzoneMaterialType.TableName + "." + strconv.Itoa(AdzoneMaterialType.Id)

	if ok, _ := clientGet.Exists(key); !ok {
		fmt.Println("the key " + key + " is not exists ")
	} else {
		fmt.Println("the key " + key + " is exists. PASSED")
	}

	if typ, err := clientGet.Type(key); err != nil || typ != "set" {
		fmt.Println("the key "+key+" type failed%s", typ)
	}

	if members, _ := clientGet.Smembers(key); len(members) != len(AdzoneMaterialType.Content.Array) {
		fmt.Println("the key " + key + " length failed")
		return
	}

	for i := 0; i < len(AdzoneMaterialType.Content.Array); i++ {
		if b, _ := clientGet.Sismember(key, []byte(AdzoneMaterialType.Content.Array[i])); b {
			fmt.Println("the key " + key + " has this value " + AdzoneMaterialType.Content.Array[i] + " PASSED")
		}
	}
}

func verifyTableDsp(messageStr string) {
	var dsp DspMessageStruct
	var dspResult DspContentStruct

	json.Unmarshal([]byte(messageStr), &dsp)

	err := clientPush.Lpush("message_queue", []byte(messageStr))

	if err != nil {
		fmt.Println("Lpush failed %s", err.Error())
	}

	//wait dispatcher process the message_queue
	time.Sleep(5 * time.Second)

	key := dsp.TableName + "." + strconv.Itoa(dsp.Id)

	if ok, _ := clientGet.Exists(key); !ok {
		fmt.Println("the key " + key + " is not exists FAILED ")
		return
	} else {
		fmt.Println("the key " + key + " is exists. PASSED")
	}

	if typ, err := clientGet.Type(key); err != nil || typ != "string" {
		fmt.Println("the key "+key+" type failed%s", typ)
		return
	}

	if values, err := clientGet.Get(key); err != nil {
		fmt.Println("get the key " + key + " value failed")
	} else {
		fmt.Fprintf(os.Stdout, "%s", values)
		json.Unmarshal(values, &dspResult)
	}

	k := reflect.TypeOf(dspResult)
	//v := reflect.ValueOf(adzoneResult)

	for i := 0; i < k.NumField(); i++ {
		fmt.Printf("%s \n", k.Field(i).Name)

		switch k.Field(i).Name {
		case "BidUrl":
			if dspResult.BidUrl == dsp.Content.BidUrl {
				fmt.Println("the key  BidUrl= " + dspResult.BidUrl + " is PASSED")
			}
		case "WinNoticeUrl":
			if dspResult.WinNoticeUrl == dsp.Content.WinNoticeUrl {
				fmt.Println("the key WinNoticeUrl= " + dspResult.WinNoticeUrl + " is PASSED")
			}
		case "WinNoticeEncrypted":
			if dspResult.WinNoticeEncrypted {
				if dspResult.WinNoticeEncrypted && dsp.Content.WinNoticeEncrypted {
					fmt.Println("the key WinNoticeEncrypted  is PASSED ", dspResult.WinNoticeEncrypted)
				}
			} else {
				if (!dspResult.WinNoticeEncrypted) && (!dsp.Content.WinNoticeEncrypted) {
					fmt.Println("the key WinNoticeEncrypted  is PASSED ", dspResult.WinNoticeEncrypted)
				}
			}

		case "Token":
			if dspResult.Token == dsp.Content.Token {
				fmt.Println("the key Token " + dspResult.Token + " is PASSED")
			}
		case "Qps":
			if dspResult.Qps == dsp.Content.Qps {
				fmt.Println("the key Qps " + strconv.Itoa(dspResult.Qps) + " is PASSED")
			}
		case "UseCookieMapping":
			if dspResult.UseCookieMapping {
				if dspResult.UseCookieMapping && dsp.Content.UseCookieMapping {
					fmt.Println("the key UseCookieMapping  is PASSED ", dspResult.UseCookieMapping)
				}
			} else {
				if (!dspResult.UseCookieMapping) && (!dsp.Content.UseCookieMapping) {
					fmt.Println("the key UseCookieMapping  is PASSED ", dspResult.UseCookieMapping)
				}
			}

		case "CookieMappingUrl":
			if dspResult.CookieMappingUrl == dsp.Content.CookieMappingUrl {
				fmt.Println("the key CookieMappingUrl " + dspResult.CookieMappingUrl + " is PASSED")
			}
		case "ParticipationBid":
			if dspResult.ParticipationBid {
				if dspResult.ParticipationBid && dsp.Content.ParticipationBid {
					fmt.Println("the key ParticipationBid  is PASSED ", dspResult.ParticipationBid)
				}
			} else {
				if (!dspResult.ParticipationBid) && (!dsp.Content.ParticipationBid) {
					fmt.Println("the key ParticipationBid  is PASSED ", dspResult.ParticipationBid)
				}
			}
		case "TransformType":
			if dspResult.TransformType == dsp.Content.TransformType {
				fmt.Println("the key TransformType " + strconv.Itoa(dspResult.TransformType) + " is PASSED")
			}
		case "UseCdn":
			if dspResult.UseCdn == dsp.Content.UseCdn {
				fmt.Println("the key UseCdn " + strconv.Itoa(dspResult.UseCdn) + " is PASSED")
			}
		case "AuctionType":
			if dspResult.AuctionType == dsp.Content.AuctionType {
				fmt.Println("the key AuctionType " + strconv.Itoa(dspResult.AuctionType) + " is PASSED")
			}
		case "MultiDeal":
			if dspResult.MultiDeal {
				if dspResult.MultiDeal && dsp.Content.MultiDeal {
					fmt.Println("the key MultiDeal  is PASSED ", dspResult.MultiDeal)
				}
			} else {
				if (!dspResult.MultiDeal) && (!dsp.Content.MultiDeal) {
					fmt.Println("the key MultiDeal  is PASSED ", dspResult.MultiDeal)
				}
			}
		case "IconUrl":
			if dspResult.IconUrl == dsp.Content.IconUrl {
				fmt.Println("the key IconUrl" + dspResult.IconUrl + " is PASSED")
			}
		case "ShowAd":
			if dspResult.ShowAd == dsp.Content.ShowAd {
				fmt.Println("the key ShowAd " + strconv.Itoa(dspResult.ShowAd) + " is PASSED")
			}

		default:
			fmt.Println("the key " + k.Field(i).Name + " not supported now")
		}
	}

}

func verifyTableMaterial(messageStr string) {
	var material MaterialMessageStruct
	var materialResult MaterialContentStruct

	json.Unmarshal([]byte(messageStr), &material)

	err := clientPush.Lpush("message_queue", []byte(messageStr))

	if err != nil {
		fmt.Println("Lpush failed %s", err.Error())
	}

	//wait dispatcher process the message_queue
	time.Sleep(5 * time.Second)

	key := material.TableName + "." + strconv.Itoa(material.Id)

	if ok, _ := clientGet.Exists(key); !ok {
		fmt.Println("the key " + key + " is not exists FAILED ")
		return
	} else {
		fmt.Println("the key " + key + " is exists. PASSED")
	}

	if typ, err := clientGet.Type(key); err != nil || typ != "string" {
		fmt.Println("the key "+key+" type failed%s", typ)
		return
	}

	if values, err := clientGet.Get(key); err != nil {
		fmt.Println("get the key " + key + " value failed")
	} else {
		fmt.Fprintf(os.Stdout, "%s", values)
		json.Unmarshal(values, &materialResult)
	}

	k := reflect.TypeOf(materialResult)

	for i := 0; i < k.NumField(); i++ {
		fmt.Printf("%s \n", k.Field(i).Name)

		switch k.Field(i).Name {
		case "Url":
			if materialResult.Url == material.Content.Url {
				fmt.Println("the key  Url= " + materialResult.Url + " is PASSED")
			}
		case "CdnUrl":
			if materialResult.CdnUrl == material.Content.CdnUrl {
				fmt.Println("the key CdnUrl= " + materialResult.CdnUrl + " is PASSED")
			}
		case "PcGaoQingUrl":
			if materialResult.PcGaoQingUrl == material.Content.PcGaoQingUrl {
				fmt.Println("the key PcGaoQingUrl= " + materialResult.PcGaoQingUrl + " is PASSED")
			}

		case "Tv1080Url":
			if materialResult.Tv1080Url == material.Content.Tv1080Url {
				fmt.Println("the key Tv1080Url " + materialResult.Tv1080Url + " is PASSED")
			}
		case "MobileLiuchangUrl":
			if materialResult.MobileLiuchangUrl == material.Content.MobileLiuchangUrl {
				fmt.Println("the key MobileLiuchangUrl " + materialResult.MobileLiuchangUrl + " is PASSED")
			}
		case "AdvertiserId":
			if materialResult.AdvertiserId == material.Content.AdvertiserId {
				fmt.Println("the key AdvertiserId " + strconv.Itoa(materialResult.AdvertiserId) + " is PASSED")
			}

		case "RollType":
			if len(materialResult.RollType) != len(material.Content.RollType) {
				fmt.Println("the key RollType is FAILED")
				continue
			}

			for i := 0; i < len(materialResult.RollType); i++ {
				if materialResult.RollType[i] == material.Content.RollType[i] {
					fmt.Println("the key RollType " + strconv.Itoa(i) + " " + strconv.Itoa(materialResult.RollType[i]) + " is PASSED")
				}
			}

		case "IsEbusinessGame":
			if materialResult.IsEbusinessGame {
				if materialResult.IsEbusinessGame && material.Content.IsEbusinessGame {
					fmt.Println("the key IsEbusinessGame  is PASSED ", materialResult.IsEbusinessGame)
				}
			} else {
				if (!materialResult.IsEbusinessGame) && (!material.Content.IsEbusinessGame) {
					fmt.Println("the key IsEbusinessGame  is PASSED ", materialResult.IsEbusinessGame)
				}
			}
		case "IndustryId":
			if materialResult.IndustryId == material.Content.IndustryId {
				fmt.Println("the key IndustryId " + strconv.Itoa(materialResult.IndustryId) + " is PASSED")
			}
		case "IsDefault":
			if materialResult.IsDefault {
				if materialResult.IsDefault && material.Content.IsDefault {
					fmt.Println("the key IsDefault  is PASSED ", materialResult.IsDefault)
				}
			} else {
				if (!materialResult.IsDefault) && (!material.Content.IsDefault) {
					fmt.Println("the key IsDefault  is PASSED ", materialResult.IsDefault)
				}
			}
		case "Bidfloor":
			if materialResult.Bidfloor == material.Content.Bidfloor {
				fmt.Println("the key Bidfloor " + strconv.Itoa(materialResult.Bidfloor) + " is PASSED")
			}
		case "DspId":
			if materialResult.DspId == material.Content.DspId {
				fmt.Println("the key DspId " + strconv.Itoa(materialResult.DspId) + " is PASSED")
			}
		case "Width":
			if materialResult.Width == material.Content.Width {
				fmt.Println("the key Width " + strconv.Itoa(materialResult.Width) + " is PASSED")
			}
		case "Height":
			if materialResult.Height == material.Content.Height {
				fmt.Println("the key Height " + strconv.Itoa(materialResult.Height) + " is PASSED")
			}
		case "Type":
			if materialResult.Type == material.Content.Type {
				fmt.Println("the key Type" + materialResult.Type + " is PASSED")
			}
		case "Duration":
			if materialResult.Duration == material.Content.Duration {
				fmt.Println("the key Duration " + strconv.Itoa(materialResult.Duration) + " is PASSED")
			}

		case "Cm":
			if len(materialResult.Cm) != len(material.Content.Cm) {
				fmt.Println("the key Cm is FAILED")
				continue
			}

			for i := 0; i < len(materialResult.Cm); i++ {
				if materialResult.Cm[i] == material.Content.Cm[i] {
					fmt.Println("the key Cm " + strconv.Itoa(i) + " " + materialResult.Cm[i] + " is PASSED")
				}
			}
		case "Pm":
			if len(materialResult.Pm) != len(material.Content.Pm) {
				fmt.Println("the key Pm is FAILED")
				continue
			}

			for i := 0; i < len(materialResult.Pm); i++ {
				if materialResult.Pm[i] == material.Content.Pm[i] {
					fmt.Println("the key Pm " + strconv.Itoa(i) + " " + materialResult.Pm[i] + " is PASSED")
				}
			}

		case "LandingPage":
			if len(materialResult.LandingPage) != len(material.Content.LandingPage) {
				fmt.Println("the key LandingPage is FAILED")
				continue
			}

			for i := 0; i < len(materialResult.LandingPage); i++ {
				if materialResult.LandingPage[i] == material.Content.LandingPage[i] {
					fmt.Println("the key LandingPage " + strconv.Itoa(i) + " " + materialResult.LandingPage[i] + " is PASSED")
				}
			}
		default:
			fmt.Println("the key " + k.Field(i).Name + " not supported now")
		}
	}

}

func verifyTableMedia(messageStr string) {
	var media MediaMessageStruct
	var mediaResult MediaContentStruct

	json.Unmarshal([]byte(messageStr), &media)

	err := clientPush.Lpush("message_queue", []byte(messageStr))

	if err != nil {
		fmt.Println("Lpush failed %s", err.Error())
	}

	//wait dispatcher process the message_queue
	time.Sleep(5 * time.Second)

	key := media.TableName + "." + strconv.Itoa(media.Id)

	if ok, _ := clientGet.Exists(key); !ok {
		fmt.Println("the key " + key + " is not exists FAILED ")
		return
	} else {
		fmt.Println("the key " + key + " is exists. PASSED")
	}

	if typ, err := clientGet.Type(key); err != nil || typ != "string" {
		fmt.Println("the key "+key+" type failed%s", typ)
		return
	}

	if values, err := clientGet.Get(key); err != nil {
		fmt.Println("get the key " + key + " value failed")
	} else {
		//fmt.Fprintf(os.Stdout, "%s \n", values)
		json.Unmarshal(values, &mediaResult)
	}

	k := reflect.TypeOf(mediaResult)
	//v := reflect.ValueOf(adzoneResult)

	for i := 0; i < k.NumField(); i++ {
		fmt.Printf("%s \n", k.Field(i).Name)

		switch k.Field(i).Name {
		case "Name":
			if mediaResult.Name == media.Content.Name {
				fmt.Println("the key  Name= " + mediaResult.Name + " is PASSED")
			}
		case "Url":
			if mediaResult.Url == media.Content.Url {
				fmt.Println("the key Url= " + mediaResult.Url + " is PASSED")
			}
		case "UseUrlVerify":
			if mediaResult.UseUrlVerify {
				if mediaResult.UseUrlVerify && media.Content.UseUrlVerify {
					fmt.Println("the key UseUrlVerify  is PASSED ", mediaResult.UseUrlVerify)
				}
			} else {
				if (!mediaResult.UseUrlVerify) && (!media.Content.UseUrlVerify) {
					fmt.Println("the key UseUrlVerify  is PASSED ", mediaResult.UseUrlVerify)
				}
			}
		default:
			fmt.Println("the key " + k.Field(i).Name + " not supported now")
		}
	}

}

func verifyTableTrafficTargeting(messageStr string) {
	var trafficTargeting TrafficTargetingMessageStruct
	var trafficTargetingResult TrafficTargetingContentStruct

	json.Unmarshal([]byte(messageStr), &trafficTargeting)

	err := clientPush.Lpush("message_queue", []byte(messageStr))

	if err != nil {
		fmt.Println("Lpush failed %s", err.Error())
	}

	//wait dispatcher process the message_queue
	time.Sleep(5 * time.Second)

	key := trafficTargeting.TableName + "." + strconv.Itoa(trafficTargeting.Id)

	if ok, _ := clientGet.Exists(key); !ok {
		fmt.Println("the key " + key + " is not exists FAILED ")
		return
	} else {
		fmt.Println("the key " + key + " is exists. PASSED")
	}

	if typ, err := clientGet.Type(key); err != nil || typ != "string" {
		fmt.Println("the key "+key+" type failed%s", typ)
		return
	}

	if values, err := clientGet.Get(key); err != nil {
		fmt.Println("get the key " + key + " value failed")
	} else {
		//fmt.Fprintf(os.Stdout, "%s \n", values)
		json.Unmarshal(values, &trafficTargetingResult)
	}

	k := reflect.TypeOf(trafficTargetingResult)
	//v := reflect.ValueOf(adzoneResult)

	for i := 0; i < k.NumField(); i++ {
		fmt.Printf("%s \n", k.Field(i).Name)

		switch k.Field(i).Name {
		case "BidType":
			if trafficTargetingResult.BidType == trafficTargeting.Content.BidType {
				fmt.Println("the key  BidType= " + strconv.Itoa(trafficTargetingResult.BidType) + " is PASSED")
			}
		case "BidPrice":
			if trafficTargetingResult.BidPrice == trafficTargeting.Content.BidPrice {
				fmt.Println("the key  BidPrice= " + strconv.Itoa(trafficTargetingResult.BidPrice) + " is PASSED")
			}
		case "ImpressionLimit":
			if trafficTargetingResult.ImpressionLimit == trafficTargeting.Content.ImpressionLimit {
				fmt.Println("the key  ImpressionLimit= " + strconv.Itoa(trafficTargetingResult.ImpressionLimit) + " is PASSED")
			}
		case "MaterialAudit":
			if trafficTargetingResult.MaterialAudit == trafficTargeting.Content.MaterialAudit {
				fmt.Println("the key  MaterialAudit= " + strconv.Itoa(trafficTargetingResult.MaterialAudit) + " is PASSED")
			}
		case "VisiableMonitor":
			if trafficTargetingResult.VisiableMonitor == trafficTargeting.Content.VisiableMonitor {
				fmt.Println("the key  VisiableMonitor= " + strconv.Itoa(trafficTargetingResult.VisiableMonitor) + " is PASSED")
			}
		case "StartDate":
			if trafficTargetingResult.StartDate == trafficTargeting.Content.StartDate {
				fmt.Println("the key  StartDate= " + strconv.Itoa(trafficTargetingResult.StartDate) + " is PASSED")
			}
		case "EndDate":
			if trafficTargetingResult.EndDate == trafficTargeting.Content.EndDate {
				fmt.Println("the key  EndDate= " + strconv.Itoa(trafficTargetingResult.EndDate) + " is PASSED")
			}
		case "DspId":
			if trafficTargetingResult.DspId == trafficTargeting.Content.DspId {
				fmt.Println("the key  DspId= " + strconv.Itoa(trafficTargetingResult.DspId) + " is PASSED")
			}
		case "AdzoneIdList":
			if len(trafficTargetingResult.AdzoneIdList) != len(trafficTargeting.Content.AdzoneIdList) {
				fmt.Println("the key AdzoneIdList is FAILED")
				continue
			}

			for i := 0; i < len(trafficTargetingResult.AdzoneIdList); i++ {
				if trafficTargetingResult.AdzoneIdList[i] == trafficTargeting.Content.AdzoneIdList[i] {
					fmt.Println("the key AdzoneIdList " + strconv.Itoa(i) + " " + strconv.Itoa(trafficTargetingResult.AdzoneIdList[i]) + " is PASSED")
				}
			}
		case "TargetingUnits":
			if len(trafficTargetingResult.TargetingUnits) != len(trafficTargeting.Content.TargetingUnits) {
				fmt.Println("the key TargetingUnits is FAILED")
				return
			}

			for i := 0; i < len(trafficTargetingResult.TargetingUnits); i++ {
				if trafficTargetingResult.TargetingUnits[i].TargetingType == trafficTargeting.Content.TargetingUnits[i].TargetingType {
					fmt.Println("the key  TargetingUnits TargetingType= " + strconv.Itoa(trafficTargetingResult.TargetingUnits[i].TargetingType) + " is PASSED")
				}
				if trafficTargetingResult.TargetingUnits[i].ValueId == trafficTargeting.Content.TargetingUnits[i].ValueId {
					fmt.Println("the key  TargetingUnits ValueId= " + strconv.Itoa(trafficTargetingResult.TargetingUnits[i].ValueId) + " is PASSED")
				}
				if trafficTargetingResult.TargetingUnits[i].IsPositive {
					if trafficTargetingResult.TargetingUnits[i].IsPositive && trafficTargeting.Content.TargetingUnits[i].IsPositive {
						fmt.Println("the key TargetingUnits IsPositive  is PASSED ", trafficTargetingResult.TargetingUnits[i].IsPositive)
					}
				} else {
					if (!trafficTargetingResult.TargetingUnits[i].IsPositive) && (!trafficTargeting.Content.TargetingUnits[i].IsPositive) {
						fmt.Println("the key TargetingUnits IsPositive  is PASSED ", trafficTargetingResult.TargetingUnits[i].IsPositive)
					}
				}
			}
		default:
			fmt.Println("the key " + k.Field(i).Name + " not supported now")
		}
	}
}

func verifyTableAdvertiser(messageStr string) {
	var advertiser AdvertiserMessageStruct
	var advertiserResult AdvertiserContentStruct

	json.Unmarshal([]byte(messageStr), &advertiser)

	err := clientPush.Lpush("message_queue", []byte(messageStr))

	if err != nil {
		fmt.Println("Lpush failed %s", err.Error())
	}

	//wait dispatcher process the message_queue
	time.Sleep(5 * time.Second)

	key := advertiser.TableName + "." + strconv.Itoa(advertiser.Id)

	if ok, _ := clientGet.Exists(key); !ok {
		fmt.Println("the key " + key + " is not exists FAILED ")
		return
	} else {
		fmt.Println("the key " + key + " is exists. PASSED")
	}

	if typ, err := clientGet.Type(key); err != nil || typ != "string" {
		fmt.Println("the key "+key+" type failed%s", typ)
		return
	}

	if values, err := clientGet.Get(key); err != nil {
		fmt.Println("get the key " + key + " value failed")
	} else {
		//fmt.Fprintf(os.Stdout, "%s \n", values)
		json.Unmarshal(values, &advertiserResult)
	}

	k := reflect.TypeOf(advertiserResult)
	//v := reflect.ValueOf(adzoneResult)

	for i := 0; i < k.NumField(); i++ {
		fmt.Printf("%s \n", k.Field(i).Name)

		switch k.Field(i).Name {
		case "CustomerType":
			if len(advertiserResult.CustomerType) != len(advertiser.Content.CustomerType) {
				fmt.Println("the key CustomerType is FAILED")
				continue
			}

			for i := 0; i < len(advertiserResult.CustomerType); i++ {
				if advertiserResult.CustomerType[i] == advertiser.Content.CustomerType[i] {
					fmt.Println("the key CustomerType " + strconv.Itoa(i) + " " + strconv.Itoa(advertiserResult.CustomerType[i]) + " is PASSED")
				}
			}
		case "AddBlackList":
			if advertiserResult.AddBlackList {
				if advertiserResult.AddBlackList && advertiser.Content.AddBlackList {
					fmt.Println("the key AddBlackList  is PASSED ", advertiserResult.AddBlackList)
				}
			} else {
				if (!advertiserResult.AddBlackList) && (!advertiser.Content.AddBlackList) {
					fmt.Println("the key AddBlackList  is PASSED ", advertiserResult.AddBlackList)
				}
			}
		default:
			fmt.Println("the key " + k.Field(i).Name + " not supported now")
		}
	}
}

func verifyTablePricePolicy(messageStr string) {
	var pricePolicy PricePolicyMessageStruct
	var pricePolicyResult PricePolicyContentStruct

	json.Unmarshal([]byte(messageStr), &pricePolicy)

	err := clientPush.Lpush("message_queue", []byte(messageStr))

	if err != nil {
		fmt.Println("Lpush failed %s", err.Error())
	}

	//wait dispatcher process the message_queue
	time.Sleep(5 * time.Second)

	key := pricePolicy.TableName + "." + strconv.Itoa(pricePolicy.Id)

	if ok, _ := clientGet.Exists(key); !ok {
		fmt.Println("the key " + key + " is not exists FAILED ")
		return
	} else {
		fmt.Println("the key " + key + " is exists. PASSED")
	}

	if typ, err := clientGet.Type(key); err != nil || typ != "string" {
		fmt.Println("the key "+key+" type failed%s", typ)
		return
	}

	if values, err := clientGet.Get(key); err != nil {
		fmt.Println("get the key " + key + " value failed")
	} else {
		//fmt.Fprintf(os.Stdout, "%s \n", values)
		json.Unmarshal(values, &pricePolicyResult)
	}

	k := reflect.TypeOf(pricePolicyResult)
	//v := reflect.ValueOf(adzoneResult)

	for i := 0; i < k.NumField(); i++ {
		fmt.Printf("%s \n", k.Field(i).Name)

		switch k.Field(i).Name {
		case "Price":
			if pricePolicyResult.Price == pricePolicy.Content.Price {
				fmt.Println("the key  Price= " + strconv.Itoa(pricePolicyResult.Price) + " is PASSED")
			}
		case "CustomerType":
			if pricePolicyResult.CustomerType == pricePolicy.Content.CustomerType {
				fmt.Println("the key CustomerType= " + strconv.Itoa(pricePolicyResult.CustomerType) + " is PASSED")
			}
		case "AdzoneIdList":
			if len(pricePolicyResult.AdzoneIdList) != len(pricePolicy.Content.AdzoneIdList) {
				fmt.Println("the key AdzoneIdList is FAILED")
				continue
			}

			for i := 0; i < len(pricePolicyResult.AdzoneIdList); i++ {
				if pricePolicyResult.AdzoneIdList[i] == pricePolicy.Content.AdzoneIdList[i] {
					fmt.Println("the key AdzoneIdList " + strconv.Itoa(i) + " " + strconv.Itoa(pricePolicyResult.AdzoneIdList[i]) + " is PASSED")
				}
			}
		case "TargetingUnits":
			if len(pricePolicyResult.TargetingUnits) != len(pricePolicy.Content.TargetingUnits) {
				fmt.Println("the key TargetingUnits is FAILED")
				return
			}

			for i := 0; i < len(pricePolicyResult.TargetingUnits); i++ {
				if pricePolicyResult.TargetingUnits[i].TargetingType == pricePolicy.Content.TargetingUnits[i].TargetingType {
					fmt.Println("the key  TargetingUnits TargetingType= " + strconv.Itoa(pricePolicyResult.TargetingUnits[i].TargetingType) + " is PASSED")
				}
				if pricePolicyResult.TargetingUnits[i].ValueId == pricePolicy.Content.TargetingUnits[i].ValueId {
					fmt.Println("the key  TargetingUnits ValueId= " + strconv.Itoa(pricePolicyResult.TargetingUnits[i].ValueId) + " is PASSED")
				}
				if pricePolicyResult.TargetingUnits[i].IsPositive {
					if pricePolicyResult.TargetingUnits[i].IsPositive && pricePolicy.Content.TargetingUnits[i].IsPositive {
						fmt.Println("the key TargetingUnits IsPositive  is PASSED ", pricePolicyResult.TargetingUnits[i].IsPositive)
					}
				} else {
					if (!pricePolicyResult.TargetingUnits[i].IsPositive) && (!pricePolicy.Content.TargetingUnits[i].IsPositive) {
						fmt.Println("the key TargetingUnits IsPositive  is PASSED ", pricePolicyResult.TargetingUnits[i].IsPositive)
					}
				}
			}
		default:
			fmt.Println("the key " + k.Field(i).Name + " not supported now")
		}
	}
}

func readTestCase() {
	var messageStruct CommonMessageStruct

	inputFile, err := os.Open("./test.json")

	if err != nil {
		println("open test case failed")
	}

	defer inputFile.Close()

	// bytes, err := ioutil.ReadFile("./test.json")
	// if err != nil {
	//     fmt.Println("ReadFile: ", err.Error())
	//     return
	// }

	// if err := json.Unmarshal(bytes, &message); err != nil {
	//     fmt.Println("Unmarshal: ", err.Error())
	//     return
	// }

	// fmt.Println(message)

	// fmt.Println(message.TableName)
	// fmt.Println(message.Id)
	// fmt.Println(message.Status)
	// fmt.Println(message.Content)
	inputReader := bufio.NewReader(inputFile)

	for {
		messageStr, err := inputReader.ReadString('\n')

		json.Unmarshal([]byte(messageStr), &messageStruct)

		//fmt.Println(message)
		// fmt.Println("table_name = " + messageStruct.TableName)
		// fmt.Println(messageStruct.Id)
		// fmt.Println("status = " + messageStruct.Status)

		switch messageStruct.TableName {

		case "adzone":

			fmt.Println("verify the table adzone")
			verifyTableAdzone(messageStr)
		case "adzone_blacklist":

			fmt.Println("verify the table adzone_blacklist")
			verifyTableAdzoneBlacklist(messageStr)

		case "adzone_material_type":

			fmt.Println("verify the table adzone_material_type")
			verifyTableAdzoneMaterialType(messageStr)

		case "dsp":

			fmt.Println("verify the table dsp")
			verifyTableDsp(messageStr)

		case "material":

			fmt.Println("verify the table material")
			verifyTableMaterial(messageStr)

		case "media":
			fmt.Println("verify the table media")
			verifyTableMedia(messageStr)
		case "traffic_targeting":
			fmt.Println("verify the table traffic_targeting")
			verifyTableTrafficTargeting(messageStr)
		case "advertiser":
			fmt.Println("verify the table advertiser")
			verifyTableAdvertiser(messageStr)
		case "price_policy":
			fmt.Println("verify the table price_policy")
			verifyTablePricePolicy(messageStr)
		default:
			fmt.Println("not support this table")
		}

		if err == io.EOF {
			return
		}
	}
}

func usage() { println("redis-dump [-p port] [-db num]") }

var lpushPort = 6379
var getPort = 6379
var clientPush redis.Client
var clientGet redis.Client

func main() {

	var err error

	args := os.Args[1:]

	for i := 0; i < len(args); i++ {
		arg := args[i]
		if arg == "-pp" && i < len(args)-1 {
			if lpushPort, err = strconv.Atoi(args[i+1]); err != nil {
				println("%s\n", err)
				return
			}
			i += 1
			continue
		} else if arg == "-gp" && i < len(args)-1 {
			if getPort, err = strconv.Atoi(args[i+1]); err != nil {
				println("%s\n", err)
				return
			}
			i += 1
			continue
		} else {
			println("Invalid argument: ", arg)
			usage()
			return
		}
	}

	if lpushPort != 0 {
		clientPush.Addr = "127.0.0.1:" + strconv.Itoa(lpushPort)
		clientPush.Db = 0
	}

	if getPort != 0 {
		clientGet.Addr = "127.0.0.1:" + strconv.Itoa(getPort)
		clientGet.Db = 0
	}

	// err = clientGet.Flush(true)

	// if err != nil {
	//     fmt.Println("clientGet flush failed")
	// }

	readTestCase()
	println(lpushPort)
	println(getPort)
}
